// console.log("users.js文件");
import userAxios from '../../api/request'
import art from "./index.art";
import {removeToken,removeUser} from '../../utils/storageUtils'

async function changePage(page=1) {
    // let page = 1;
    let limit = 5;
    // axios发送请求
    let {data:{users,total}} = await userAxios({
        url: `/admin/user/${page}/${limit}`,
    })
    let countPage = Math.ceil(total/5);
    let html = art({
        username: "laowang",
        users,
        total,
        page,
        countPage
    })
    // 渲染模板
    document.getElementById('app').innerHTML = html;

    logoutFn();
}

changePage(1)
// 函数-全局函数
window.changePage = changePage;

async function deleteUser(uid,page){
    // console.log(uid);
    if(confirm('确定要删除此用户吗？')){
         // 发送删除请求
        let res =  await userAxios({
            method:'delete',
            url:`/admin/user/${uid}`
        })
        if(res.code===2000){
            alert('删除成功');
            window.location.href = `./users.html`
        }else{
            alert('删除失败')
        }
    }
}
window.deleteUser = deleteUser;

function logoutFn(){
    // 退出登录
    let logoutbtn = document.querySelector('#logoutbtn');
    logoutbtn.onclick = function(){
        alert('退出成功')
        removeToken();
        removeUser();
        window.location.href = 'login.html'
    }
}